﻿using System.Collections.Generic;
using UnityEngine;

public class IWorldObject
{
    private static int applyId = 10000;
    private readonly int id;
    private readonly OwnerObj selfOwnerObj;

    protected UnityGameObject unityGameObject;

    private float existTime = 0.0f;

    private IStage stage = null;

    protected string prefabName;
    private Vector3 spawnPos = Vector3.zero;
    private readonly Quaternion rotation = Quaternion.identity;

    private bool isDead = false;

    public IWorldObject(IStage stage)
    {
        id = ++applyId;
        selfOwnerObj = OwnerObj.CreateNew(this);
        this.stage = stage;
    }

    public IWorldObject(IStage stage, string prefabName) {
        id = ++applyId;
        selfOwnerObj = OwnerObj.CreateNew(this);
        this.stage = stage;

        this.prefabName = prefabName;
    }

    public IWorldObject(IStage stage, string prefabName, Vector3 spawnPos)
    {
        id = ++applyId;
        selfOwnerObj = OwnerObj.CreateNew(this);
        this.stage = stage;

        this.prefabName = prefabName;
        this.spawnPos = spawnPos;
    }

    private IWorldObject(IStage stage, string prefabName, Vector3 spawnPos, Quaternion rotation)
    {
        id = ++applyId;
        selfOwnerObj = OwnerObj.CreateNew(this);
        this.stage = stage;

        this.prefabName = prefabName;
        this.spawnPos = spawnPos;
        this.rotation = rotation;
    }


    /// <summary>
    /// 作为永久者的对象
    /// </summary>
    /// <returns></returns>
    public OwnerObj GetSelfOwnerObj()
    {
        return selfOwnerObj;
    }

    /// <summary>
    /// 开始
    /// </summary>
    public virtual void Start()
    {
        if (null != stage)
        {
            stage.AddWorldObj(this);

            if (null == unityGameObject)
            {
                unityGameObject = new UnityGameObject();
            }

            unityGameObject.Instantiate(prefabName, spawnPos, rotation);
        }
    }

    public virtual void Die()
    {
        isDead = true;
    }

    /// <summary>
    /// 销毁
    /// </summary>
    public virtual void Destroy()
    {
        if (null != stage)
        {
            stage.RemoveWorldObj(GetId());
        }

        if (null != unityGameObject)
        {
            unityGameObject.Destroy();
            unityGameObject = null;
        }
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="deltaTime"></param>
    public virtual void Update(float deltaTime)
    {
        existTime += deltaTime;

        if (isDead)
        {
            Destroy();
        }
    }

    /// <summary>
    /// 获取id
    /// </summary>
    /// <returns></returns>
    public int GetId()
    {
        return id;
    }

    /// <summary>
    /// 获取IStage
    /// </summary>
    /// <returns></returns>
    public IStage GetStage()
    {
        return stage;
    }

    /// <summary>
    /// 获取UnityGameObject
    /// </summary>
    /// <returns></returns>
    public UnityGameObject GetUnityGameObject()
    {
        return unityGameObject;
    }

    /// <summary>
    /// 设置位置
    /// </summary>
    /// <param name="position"></param>
    public void SetPosition(Vector3 position) {
        if (null == unityGameObject)
        {
            spawnPos = position;
            return;
        }

        unityGameObject.SetPosition(position);
    }

    /// <summary>
    /// 获取位置
    /// </summary>
    /// <returns></returns>
    public Vector3 GetPosition()
    {
        if (null == unityGameObject)
        {
            return Vector3.zero;
        }

        return unityGameObject.GetPosition();
    }

}
